package com.phkj.qdh.ui.fragment.baseinspect

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.phkj.qdh.databinding.FragmentBaseInspectSelectBinding
import com.phkj.qdh.ui.adapter.InspectTypeSelectAdapter
import com.phkj.qdh.ui.fragment.teaminspect.TeamInspectVM
import com.phkj.qdh.ui.fragment.teaminspect.getTeamInspectVMProvider
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 基础巡查选择
 */
class BaseInspectSelectFragment : Fragment() {
    private var param1: String? = null
    private var param2: String? = null
    private var _binding: FragmentBaseInspectSelectBinding? = null
    private val binding get() = _binding!!
    private val inspectTypeSelectAdapter by lazy { InspectTypeSelectAdapter() }
    private lateinit var vm: TeamInspectVM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }

        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentBaseInspectSelectBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getTeamInspectVMProvider())[TeamInspectVM::class.java]
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            rvBaseType.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = inspectTypeSelectAdapter
                addItemDecoration(MyItemDecoration(32))
            }
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack("base", FragmentManager.POP_BACK_STACK_INCLUSIVE)
            }
        }

        inspectTypeSelectAdapter.setOnItemClickListener { adapter, v, pos ->
            adapter.getItem(pos)?.let {bean ->
                parentFragmentManager.commit {
                    addToBackStack("base")
                    replace(id, BaseInspectSubmitFragment.newInstance(bean, ""))
                    setReorderingAllowed(true)
                }
            }

        }
        launchAndRepeatWithViewLifecycle {
            vm.baseInspectType.collectLatest {
                inspectTypeSelectAdapter.submitList(it.get(0).childrenList)
            }
        }

        vm.getBaseInspectType()

    }

    companion object {
        /**
         *
         */
        @JvmStatic
        fun newInstance(param1: String, param2: String) = BaseInspectSelectFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}